import router from './router'
import store from './store'
import { getTokenTime } from '@/utils/auth'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

// 白名单
const whiteList = ['/login', '/404']
// 获取token的时间戳
const startTime = getTokenTime()
// 超时时间
const lostToken = 60 * 60 * 2
// 前置路由守卫

const goto = (x, y) => {
  x(`${y}`)
  NProgress.done()
}
router.beforeEach((to, from, next) => {
  NProgress.start()
  const nowTime = Date.now()
  if (!store.state.user.token) return whiteList.includes(to.path) ? next() : goto(next, '/login')

  if ((nowTime - startTime) / 1000 > lostToken) return goto(next, '/login')
  if (to.path !== '/login') return next()
  goto(next, '/')
})

router.afterEach(() => {
  NProgress.done()
})

